using System.Data;
using System.Text;

namespace TimeSheetBLL.Brokers
{
    /// <summary>
    /// Summary description for ProjectBroker.
    /// </summary>
    public class ProjectBroker : Broker
    {
        public ProjectBroker() : base()
        {
        }

        public override DataTable SelectAll(IDBEntity entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(
                "SELECT project_name, project_id, client_name, project.client_id, projectstatus.status_id, concat(client_name, ' - ', project_name) AS client_project, status_name  ");
            sb.Append("FROM client, project, projectstatus ");
            sb.Append("WHERE client.client_id = project.client_id ");
            sb.Append("AND project.status_id = projectstatus.status_id ");
            sb.Append("AND project.active = 1 ");
            sb.Append("ORDER BY client_name, project_name ");

            return data.GetTable(sb.ToString());
        }

        public void Fill(Project proj)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(
                "SELECT project_id, project.client_id, project_name, status_id, project.active, client.client_name ");
            sb.Append("FROM client, project ");
            sb.Append("WHERE client.client_id = project.client_id ");
            sb.Append("AND project_id = ").Append(proj.ID);

            data.Fill(proj, sb.ToString());
        }

        public override bool Delete(IDBEntity entity)
        {
            string statement = "UPDATE project SET active = 0 WHERE project_id = " +
                               entity.ID;
            return data.ExecuteNonQuery(statement) > 0;
        }
    } //class
} //namespace